package com.gzs.learn.admin.config;

import com.gzs.learn.admin.common.enums.DataSourceType;
import com.gzs.learn.common.datasource.DynamicDataSource;
import com.gzs.learn.inf.GlobalConstant;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * druid 配置多数据源
 *
 * @author guanzhisong
 */
@Configuration
@Profile(GlobalConstant.NOT_PROFILE_TEST_ENV)
public class DatasourceConfig {
    @ConfigurationProperties(prefix = "spring.datasource.master")
    @Bean(name = "masterDataSource")
    public DataSource masterDataSource() {
        return new HikariDataSource();
    }

    @ConfigurationProperties(prefix = "spring.datasource.master-slave")
    @Bean(name = "masterSlaveDataSource")
    public DataSource masterSlaveDataSource() {
        return new HikariDataSource();
    }

    @ConfigurationProperties(prefix = "spring.datasource.qrtz")
    @Bean(name = "qrtzDataSource")
    public DataSource qrtzDataSource() {
        return new HikariDataSource();
    }

    @ConfigurationProperties(prefix = "spring.datasource.qrtz-slave")
    @Bean(name = "qrtzSlaveDataSource")
    public DataSource qrtzSlaveDataSource() {
        return new HikariDataSource();
    }

    @ConfigurationProperties(prefix = "spring.datasource.log")
    @Bean(name = "logDataSource")
    public DataSource logDataSource() {
        return new HikariDataSource();
    }

    @ConfigurationProperties(prefix = "spring.datasource.log-slave")
    @Bean(name = "logSlaveDataSource")
    public DataSource logSlaveDataSource() {
        return new HikariDataSource();
    }

    @Bean
    @Primary
    public DataSource dataSource(@Qualifier("masterDataSource") DataSource masterDataSource, @Qualifier("masterSlaveDataSource") DataSource masterSlaveDataSource,
                                 @Qualifier("qrtzDataSource") DataSource qrtzDataSource, @Qualifier("qrtzSlaveDataSource") DataSource qrtzSlaveDataSource,
                                 @Qualifier("logDataSource") DataSource logDataSource, @Qualifier("logSlaveDataSource") DataSource logSlaveDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>(6);
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
        targetDataSources.put(DataSourceType.SLAVE.name(), masterSlaveDataSource);
        targetDataSources.put(DataSourceType.LOG.name(), logDataSource);
        targetDataSources.put(DataSourceType.LOG_SLAVE.name(), logSlaveDataSource);
        targetDataSources.put(DataSourceType.QRTZ.name(), qrtzDataSource);
        targetDataSources.put(DataSourceType.QRTZ_SLAVE.name(), qrtzSlaveDataSource);
        return new DynamicDataSource(masterDataSource, targetDataSources);
    }
}
